/*************************************************
Author: Aven 
Date: 12/03/2013
Description:
Notes:
*************************************************/

#ifndef __CWishMgr_h__
#define __CWishMgr_h__

#include "CTArray.h"
#include "../maindef.h"
#include "commerrno.h"
#include "commclass.h"
#include "commmsg.h"
#include "MsgAct.pb.h"

//请求操作类型
enum
{
    ECONST_WISH_REQ_INFO    = 0,  //获取许愿信息
    ECONST_WISH_REQ_SILVER  = 1,  //银币许愿
    ECONST_WISH_REQ_GOLD1   = 2,  //10金币许愿
    ECONST_WISH_REQ_GOLD2   = 3,  //50金币许愿
};

class CWishConfig;

//许愿池配置信息
class CWishConfigData
{
public:
    CWishConfigData()
        :_bHasConfig(false)
    {
    }

    virtual ~CWishConfigData()
    {
    }

    void Clear() {_bHasConfig = false;}
    bool HasConfig() const {return _bHasConfig;}

public:
    bool _bHasConfig;

    int iWishType;           //许愿类型

    int iStartTimes;         //返利倍数起始值
    int iEndTimes;         //返利倍数最大值

    int iRatio;             //概率百分比
};

//许愿配置
class CWishConfig
{
public:
	CWishConfig()
    {
	}

	virtual ~CWishConfig() 
    {
	}

    int Init()
    {
        for (int i = 0; i < _astWishSilverConfig.Size(); ++i)
        {
            _astWishSilverConfig[i].Clear();
        }

        for (int i = 0; i < _astWishGolds10Config.Size(); ++i)
        {
            _astWishGolds10Config[i].Clear();
        }

        for (int i = 0; i < _astWishGolds50Config.Size(); ++i)
        {
            _astWishGolds50Config[i].Clear();
        }

        return 0;
    }

    int AddWishConfig( CWishConfigData& stData )
    {
        int iWishType = stData.iWishType;

        if (!stData.HasConfig())
        {
            return ERROR_WISH_CONFIG;
        }

        switch(iWishType)
        {
        case ECONST_WISH_REQ_SILVER  :  //银币许愿
            {
                if( this->_astWishSilverConfig.Size()>=this->_astWishSilverConfig.GetCount() )
                {
                    return ERROR_WISH_CONFIG;
                }
                this->_astWishSilverConfig.AddOneItem(stData);
            }
            break;
        case ECONST_WISH_REQ_GOLD1   :  //10金币许愿
            {
                if( this->_astWishGolds10Config.Size()>=this->_astWishGolds10Config.GetCount() )
                {
                    return ERROR_WISH_CONFIG;
                }
                this->_astWishGolds10Config.AddOneItem(stData);
            }
            break;
        case ECONST_WISH_REQ_GOLD2   :  //50金币许愿
            {
                if( this->_astWishGolds50Config.Size()>=this->_astWishGolds50Config.GetCount() )
                {
                    return ERROR_WISH_CONFIG;
                }
                this->_astWishGolds50Config.AddOneItem(stData);
            }
            break;
        default:
            return ERROR_WISH_TYPE;
        }

        return 0;
    }

    int GetWishRandomRatio( int iWishType, int& iRatioTimes ) 
    {
        int iRet = 0;
        CWishConfigData stData;

        switch(iWishType)
        {
        case ECONST_WISH_REQ_SILVER  :  //银币许愿
            {
                iRet = this->ConsumeSectionWithRatio(this->_astWishSilverConfig, stData);
                int iAddRandom = 0;
                iAddRandom = stData.iEndTimes-stData.iStartTimes+1;
                iRatioTimes = random()%iAddRandom + stData.iStartTimes;
                CHECK_RETURN(iRet);
            }
            break;
        case ECONST_WISH_REQ_GOLD1   :  //10金币许愿
            {
                iRet = this->ConsumeSectionWithRatio(this->_astWishGolds10Config, stData);
                iRatioTimes = stData.iStartTimes;
                CHECK_RETURN(iRet);
            }
            break;
        case ECONST_WISH_REQ_GOLD2   :  //50金币许愿
            {
                iRet = this->ConsumeSectionWithRatio(this->_astWishGolds50Config, stData);
                iRatioTimes = stData.iStartTimes;
                CHECK_RETURN(iRet);
            }
            break;
        default:
            return ERROR_WISH_TYPE;
        }

        return 0;
    }
    
    int ConsumeSectionWithRatio(CTLib::CTArray<CWishConfigData, MAX_WISH_NUM>& _astWish, CWishConfigData& stData)
    {
        bool bHasResult = false;

        if(_astWish.Size()==0)
        {
            return ERROR_WISH_CONFIG;
        }

        int iSumRatio = 0;
        int iArrayRatioLayer[MAX_WISH_NUM] = {0};

        for( int i=0; i<_astWish.Size(); ++i )
        {
            iSumRatio += _astWish[i].iRatio;
            iArrayRatioLayer[i] = iSumRatio;
        }

        int iResult = random()%(iSumRatio+1);
        int iIndex = 0;

        for( int i=0; i<_astWish.Size(); ++i )
        {
            if( iResult<=iArrayRatioLayer[i] )
            {
                iIndex = i;
                bHasResult = true;
                break;
            }
        }

        if( !bHasResult )
        {
            return ERROR_WISH_CONFIG;
        }

        stData = _astWish[iIndex];

        return 0;
    }

private:
    CTLib::CTArray<CWishConfigData, MAX_WISH_NUM>           _astWishSilverConfig;   //银币许愿配置
    CTLib::CTArray<CWishConfigData, MAX_WISH_NUM>           _astWishGolds10Config;   //金币10许愿配置
    CTLib::CTArray<CWishConfigData, MAX_WISH_NUM>           _astWishGolds50Config;   //金币50许愿配置
};

////////////////Mgr///////////////
class CWishMgr
{
public:

    CWishMgr()
    {
        m_bUpdateWishData = false;
        m_bUpdateMoneyData = false;
    }

    virtual ~CWishMgr()
    {
    }

    int Init()
    {
        _stWishCfg.Init();
        return 0;
    }

    int DoReq(MsgReqWish &stReq, MsgAnsWish & stAns);

    int UserReqWish(unsigned int uiGid, int iReqType, MsgAnsWish& stAns);

    //检查是否许愿过,没有的就初始化
    bool CheckValidWish(int iReqType, CWishData& stWishData);

    //初始化许愿池信息
    int InitWish(CWishData& stWishData);

    void UpdateData(unsigned int uiGid);

    //获取活动信息
    int GetWishInfo(unsigned int uiGid, MsgAnsWish & stAns);

    int LoadConfig(const char* szCfgPath);

private:
    bool m_bUpdateWishData;        //数据更新状态
    bool m_bUpdateMoneyData;        //数据更新状态

    CWishConfig _stWishCfg; 
};

#endif //__CWishMgr_h__
